<template>
<div>
  <Card>
    <Collapse style="margin-bottom:15px;">
      <Panel>查询
        <div slot="content">
          <Form ref="formLkyMajor" :model="formLkyMajor" label-position="right" :label-width="80" inline>
            <FormItem label="所属学院" prop="name1">
              <Select v-model="formLkyMajor.coid" style="width:200px"  @on-open-change="onOpenChangeCollege" clearable >
                <Option v-for="item in collegeList" :value="item.id" :key="item.id">
                  {{ item.name }}
                </Option>
              </Select>
            </FormItem>
            <FormItem label="专业名称" prop="name">
              <Input type="text" v-model="formLkyMajor.name" placeholder="专业名称" />
            </FormItem>
            <FormItem>
              <Button type="primary" @click="handleSubmit('formLkyMajor')">查询</Button>
              <Button @click="handleReset('formLkyMajor')" style="margin-left: 8px">重置</Button>
            </FormItem>
          </Form>
        </div>
      </Panel>
    </Collapse>
    <ButtonGroup style="margin-bottom: 15px;">
      <Button @click="handleOpenPage('add')">添加</Button>
      <Add v-model="isOpenAddPage" @addRow="addLkyMajor" />
      <Edit v-model="isOpenEditPage" @updateRow="updateLkyMajor" :lkyMajor="editLkyMajor" />
    <!--  <Button @click="batchDelete()">删除</Button>-->
    </ButtonGroup>
    <Table ref="tableLkyMajor" :data="tableData" :columns="columns" @on-selection-change="onSelectionChange"
     @on-sort-change="onSortChange" />
    <div style="margin: 10px;overflow: hidden">
      <div style="float: right;">
        <Page :total="total" :current="current" :page-size="pageSize" @on-page-size-change="changePageSize" @on-change="changePage"
         :show-total="true" show-sizer></Page>
      </div>
    </div>

  </Card>
</div>
</template>

<script>
import {
  list,
  del,
  batchDelete
} from '@/api/lky/lkyMajor'
// 学院查询、专业查询
import { queryAll } from '@/api/lky/lkyCollege'

import {
  createFormObj
} from '@/libs/util'

import Add from './add.vue'
import Edit from './edit.vue'

export default {
  name: 'LkyMajor',
  components: {
    Add,
    Edit
  },
  data () {
    return {
      collegeList: [],
      formLkyMajor: {
        name: '',
        major_number: '',
        coid: ''
      },
      columns: [{
        type: 'selection',
        width: 60,
        align: 'center'
      },
      {
        key: 'name',
        title: '专业名称',
        sortable: true
      },
      {
        key: 'name1',
        title: '所属学院',
        sortable: true
      },
      /*{
        key: 'major_number',
        title: '专业编号',
        sortable: true
      },*/
      {
        key: 'remark_info',
        title: '备注信息',
        sortable: true
      },
      {
        key: 'creation_time',
        title: '创建时间',
        sortable: true
      },
      {
        key: 'update_time',
        title: '修改时间',
        sortable: true
      },
      {
        title: '操作',
        key: 'action',
        width: 250,
        align: 'center',
        render: (h, params) => {
          return h('div', [
            h(
              'Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    // debugger;
                    this.editLkyMajor = params.row
                    this.handleOpenPage('edit')
                  }
                }
              },
              '编辑'
            ),
            h(
              'Button', {
                props: {
                  type: 'error',
                  size: 'small'
                },
                on: {
                  click: () => {
                    this.$Modal.confirm({
                      title: '提示',
                      content: '<p>确定删除吗?</p>',
                      onOk: () => {
                        del({
                          'lkyMajor.id': params.row.id
                        }).then(res => {
                          if (res) {
                            this.tableData.splice(params.index, 1)
														this.$Message.success('删除成功')
                          }else{
														this.$Message.error('删除失败，本专业下还有班级')
													}
                        })
                      },
                      onCancel: () => {}
                    })
                  }
                }
              },
              '删除'
            )
          ])
        }
      }
      ],
      tableData: [],
      total: 0,
      current: 1,
      pageSize: 10,
      isOpenAddPage: false,
      isOpenEditPage: false,
      editLkyMajor: {},
      selection: []
    }
  },
  methods: {
    // 查找学院
    onOpenChangeCollege () {
      queryAll().then(res => {
        this.collegeList = res.data
      })
    },
    handleSubmit (name) {
      // 查询按钮事件
      this.getLkyMajorList(this.current, this.pageSize)
      this.$Message.success('Success!')
    },
    handleReset (name) {
      this.formLkyMajor.coid = ''
      // 重置按钮事件
      this.$refs[name].resetFields()
    },
    handleOpenPage (name) {
      // 打开页面事件(添加页,编辑页...)
      if (name === 'add') this.isOpenAddPage = true
      else if (name === 'edit') {
        this.isOpenEditPage = true
      }
    },
    addLkyMajor (row) {
      this.tableData.push(row)
      this.getLkyMajorList(this.current, this.pageSize)
    },
    updateLkyMajor (row) {
      this.$set(this.tableData, row._index, row)
      this.getLkyMajorList(this.current, this.pageSize)
    },
    batchDelete () {
      this.$Modal.confirm({
        title: '提示',
        content: '<p>确定删除吗?</p>',
        onOk: () => {
          if (this.selection.length > 0) {
            let ids = ''
            for (var i = 0; i < this.selection.length; i++) {
              ids += ',' + this.selection[i].id
            }
            batchDelete({
              ids: ids
            }).then(res => {
              for (var i = 0; i < this.selection.length; i++) {
                this.tableData = this.tableData.filter(
                  item => item.id !== this.selection[i].id
                )
              }
            })
          } else {
            this.$Message.error('未选中记录!')
          }
        },
        onCancel: () => {}
      })
    },
    getLkyMajorList (current, pageSize, paras) {
      let para = {
        pageNumber: current,
        pageSize: pageSize
      }
      if (paras) Object.assign(para, paras)
      list(this.handleRequestParams(para)).then(res => {
        this.tableData = res.list
        this.current = parseInt(res.pageNumber)
        this.pageSize = parseInt(res.pageSize)
        this.total = parseInt(res.totalRow)
      })
    },
    handleRequestParams (para) {
      console.log(this.formLkyMajor)
      Object.assign(para, createFormObj(this.formLkyMajor, 'lkyMajor'))
      return para
    },
    exportExcel () {
      this.$refs.tableLkyMajor.exportCsv({
        filename: `table-${new Date().valueOf()}.csv`
      })
    },
    changePage (pageNumber) {
      this.getLkyMajorList(pageNumber, this.pageSize)
    },
    changePageSize (pageSize) {
      this.getLkyMajorList(this.current, pageSize)
    },
    remove (index) {
      this.data6.splice(index, 1)
    },
    onSelectionChange (selection) {
      this.selection = selection
    },
    onSortChange (obj) {
      if (obj.order === 'normal') {
        this.getLkyMajorList(1, this.pageSize)
      } else {
        this.getLkyMajorList(1, this.pageSize, {
          field: obj.key,
          order: obj.order
        })
      }
    }
  },
  mounted () {
    this.getLkyMajorList(this.current, this.pageSize)
  }
}
</script>

<style>
</style>
